
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;


public class Kimp extends JFrame {        
    ArrayList<Point> points = new ArrayList<Point>();
    ArrayList<Line> lines = new ArrayList<Line>();
    static ArrayList<Figure> figures= new ArrayList<Figure>();
    static Figure curFig;
        /**
    * @param args
         */
    public static void main(String[] args) {
    	Kimp k = new Kimp(600, 400);
    	k.repaint();
    }
    public Kimp(int width, int height) {
    	this.setSize(width, height);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.addMouseMotionListener(new MouseMotionAdapter() {
        	@Override
        	public void mouseDragged(MouseEvent e) {
	        	// TODO Auto-generated method stub
        		Point pnt = e.getPoint();
        		points.add(pnt);
        		curFig.updPoints(points);
        		repaint();
	        }
        });
        this.addMouseListener(new MouseAdapter() {
        	@Override
            public void mousePressed(MouseEvent e) {
        		// TODO Auto-generated metod stub
                super.mousePressed(e);
                Point pnt = e.getPoint();
                points.add(pnt);
                curFig = new Figure (points);
                figures.add(curFig);
        	}
        	public void mouseReleased(MouseEvent e) {
        		// TODO Auto-generated method stub
                super.mouseReleased(e);
                curFig = new Figure (points);
                figures.add(curFig);
                points = new ArrayList<Point>();
                repaint();
                System.out.println(figures.size());
        	}
        });
        this.addKeyListener(new KeyAdapter() {
        	@Override
            public void keyPressed(KeyEvent e) {
            // TODO Auto-generated method stub
        		super.keyPressed(e);                
        		if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
        			int result = JOptionPane.showConfirmDialog(null, "Are you sure?", "Exit?", JOptionPane.YES_NO_OPTION);
                    if (result == JOptionPane.YES_OPTION) {
                    	System.exit(0);
                     }
                 }
        	}
        });
    }
    @Override
    public void paint(Graphics g) {
    	// TODO Auto-generated method stub
    	super.paint(g);
    	if (!figures.isEmpty()){
    		Point start = figures.get(0).getLines().get(0).start;
        	Point end = figures.get(0).getLines().get(0).end;	
        	g.drawLine (start.x, start.y, end.x, end.y);
    	}
    	
    	for (Figure eachFig: figures) {
    		for (Line eLn: eachFig.getLines()) {
    			 //eachFig.System.out.println(eLn.end.x);
    			
    			g.drawLine(eLn.start.x, eLn.start.y, eLn.end.x, eLn.end.y);
    		}
    	}
    	

    	/*
    	boolean start = false;
        Point p0 = null; 
        
        
        for (Point p : points) {
        	if (start) {
        		g.drawLine(p0.x, p0.y, p.x, p.y);
        	} else {
        		start = true;
        	}
        	p0 = p;
          }
         */
    }
}